using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.UI.Page;
using MicroMsg.UI.Util;
using Microsoft.Phone.Controls;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
namespace MicroMsg.UI.UserContrl
{
	public class ChatPeopleButton : Button, IChatPeople
	{
		public enum PeopleButtonMode
		{
			commonMode,
			delMode
		}
		private const string TAG = "ChatPeopleButton";
		public RoutedEventHandler DelPepleClick;
		private Grid imgGrid;
		private EmojiText NickNameTxt;
		private Image HeadImg;
		private Image DelImg;
		private DispatcherTimer mAtTimer;
		private double maniStartTime;
		private double maniCompleteTime;
		private string cuUser;
		private ChatPeopleInfo mPeople;
		private EventWatcher mWatcherHeadImgAdd;
		public static ChatPeopleButton.PeopleButtonMode itemMode = ChatPeopleButton.PeopleButtonMode.commonMode;
		public static List<ChatPeopleButton> chatPeopleBtnList = new List<ChatPeopleButton>();
		private bool _contentLoaded;
		public ChatPeopleInfo People
		{
			get
			{
				return this.mPeople;
			}
			set
			{
				this.mPeople = value;
				this.RefreshPeople();
			}
		}
		public ChatPeopleButton()
		{
			this.InitializeComponent();
			base.SetValue(TiltEffect.IsTiltEnabledProperty, true);
			base.add_Click(new RoutedEventHandler(this.ChatPeopleButton_Click));
			base.add_Loaded(new RoutedEventHandler(this.ChatPeopleButton_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.ChatPeopleButton_Unloaded));
		}
		private void ChatPeopleButton_Unloaded(object sender, RoutedEventArgs e)
		{
			this.GetHeaderUnit();
			ChatPeopleButton.DelFromButtonList(this);
		}
		private void ChatPeopleButton_Loaded(object sender, RoutedEventArgs e)
		{
			this.GetHeaderInit();
			ChatPeopleButton.AddToButtonList(this);
		}
		private void ChatPeopleButton_Click(object sender, RoutedEventArgs e)
		{
			if (this.DelPepleClick != null)
			{
				this.DelPepleClick.Invoke(this, e);
			}
		}
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			this.NickNameTxt = (base.GetTemplateChild("NickNameTxt") as EmojiText);
			this.HeadImg = (base.GetTemplateChild("HeadImg") as Image);
			this.DelImg = (base.GetTemplateChild("DelImg") as Image);
			this.DelImg.set_Source(new ImageSourceConverter().ConvertFromString("/Source/Plugin/Sns/Common/Resource/delete_icon.png") as ImageSource);
			this.imgGrid = (base.GetTemplateChild("imgGrid") as Grid);
			if (this.imgGrid != null)
			{
				this.imgGrid.add_ManipulationStarted(new EventHandler<ManipulationStartedEventArgs>(this.imgGrid_ManipulationStarted));
				this.imgGrid.add_ManipulationCompleted(new EventHandler<ManipulationCompletedEventArgs>(this.imgGrid_ManipulationCompleted));
			}
			this.RefreshPeople();
		}
		private void imgGrid_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
		{
			DispatcherTimer dispatcherTimer = new DispatcherTimer();
			dispatcherTimer.set_Interval(new TimeSpan(0, 0, 0, 0, 1200));
			dispatcherTimer.add_Tick(new EventHandler(this.reportAtTimer_Tick));
			dispatcherTimer.Start();
			this.mAtTimer = dispatcherTimer;
			this.maniStartTime = Util.getNowMilliseconds();
		}
		private void reportAtTimer_Tick(object sender, EventArgs e)
		{
			DispatcherTimer dispatcherTimer = sender as DispatcherTimer;
			if (dispatcherTimer == null)
			{
				return;
			}
			dispatcherTimer.Stop();
			BasePage rootPage = ToolFunction.GetRootPage();
			if (!(rootPage is PeopleListPage))
			{
				return;
			}
			if (base.get_Parent() is PeopleList)
			{
				PeopleList peopleList = base.get_Parent() as PeopleList;
				peopleList.NowListMode = PeopleList.PeopleListMode.CommonModeMode;
				ChatPeopleButton.ChangeMode(ChatPeopleButton.PeopleButtonMode.delMode);
			}
		}
		private void imgGrid_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
		{
			if (this.mAtTimer != null)
			{
				this.mAtTimer.Stop();
				this.mAtTimer = null;
			}
			this.maniCompleteTime = Util.getNowMilliseconds();
			if (this.maniCompleteTime - this.maniStartTime <= 800.0)
			{
				e.set_Handled(false);
				return;
			}
			e.set_Handled(true);
		}
		private void RefreshPeople()
		{
			if (this.People == null)
			{
				return;
			}
			if (this.NickNameTxt == null)
			{
				return;
			}
			if (this.HeadImg == null)
			{
				return;
			}
			if (this.People.NickName != null)
			{
				this.NickNameTxt.Text = this.People.NickName;
			}
			this.cuUser = this.People.UserName;
			if (this.HeadImg.get_Source() == null)
			{
				this.HeadImg.set_Source(HeadImgMgr.defaultHeadImage);
			}
			PhoneApplicationFrame phoneApplicationFrame = Application.get_Current().get_RootVisual() as PhoneApplicationFrame;
			BasePage basePage = phoneApplicationFrame.get_Content() as BasePage;
			if (basePage != null && basePage.actionService != null)
			{
				basePage.actionService.Push(delegate
				{
					if (this.HeadImg != null && this.People.UserName != null)
					{
						Log.smethod_5("ChatPeopleButton", "HeadImgMgr.getHeadImage start " + this.People.UserName);
						this.HeadImg.set_Source(HeadImgMgr.getHeadImage(this.People.UserName, true));
						Log.smethod_5("ChatPeopleButton", "HeadImgMgr.getHeadImage end " + this.People.UserName);
						return;
					}
				});
			}
		}
		public void HideDelIcon()
		{
			this.DelImg.set_Visibility(1);
		}
		public void ShowDelIcon()
		{
			this.DelImg.set_Visibility(0);
		}
		private void GetHeaderInit()
		{
			this.mWatcherHeadImgAdd = new EventWatcher(this, null, new EventHandlerDelegate(this.onEventHandlerHeadImgAdd));
			EventCenter.registerEventWatcher(EventConst.ON_MGR_HEAD_IMG_READY, this.mWatcherHeadImgAdd);
		}
		private void GetHeaderUnit()
		{
			EventCenter.removeEventWatcher(EventConst.ON_MGR_HEAD_IMG_READY, this.mWatcherHeadImgAdd);
		}
		private void onEventHandlerHeadImgAdd(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			List<string> listObject = evtArgs.getListObject<string>();
			if (listObject == null)
			{
				return;
			}
			if (string.IsNullOrEmpty(this.cuUser))
			{
				return;
			}
			using (List<string>.Enumerator enumerator = listObject.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					string current = enumerator.get_Current();
					if (current != null && current.GetType() == typeof(string) && this.cuUser == current)
					{
						this.RefreshPeople();
						break;
					}
				}
			}
		}
		public static void ChangeMode(ChatPeopleButton.PeopleButtonMode toMode)
		{
			if (toMode == ChatPeopleButton.PeopleButtonMode.delMode)
			{
				ChatPeopleButton.itemMode = ChatPeopleButton.PeopleButtonMode.delMode;
				using (List<ChatPeopleButton>.Enumerator enumerator = ChatPeopleButton.chatPeopleBtnList.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						ChatPeopleButton current = enumerator.get_Current();
						if (current.People.UserName != AccountMgr.curUserName)
						{
							current.ShowDelIcon();
						}
					}
					return;
				}
			}
			ChatPeopleButton.itemMode = ChatPeopleButton.PeopleButtonMode.commonMode;
			using (List<ChatPeopleButton>.Enumerator enumerator2 = ChatPeopleButton.chatPeopleBtnList.GetEnumerator())
			{
				while (enumerator2.MoveNext())
				{
					ChatPeopleButton current2 = enumerator2.get_Current();
					if (current2.People.UserName != AccountMgr.curUserName)
					{
						current2.HideDelIcon();
					}
				}
			}
		}
		public static void AddToButtonList(ChatPeopleButton cpBtn)
		{
			if (!ChatPeopleButton.chatPeopleBtnList.Contains(cpBtn))
			{
				ChatPeopleButton.chatPeopleBtnList.Add(cpBtn);
			}
		}
		public static void DelFromButtonList(ChatPeopleButton cpBtn)
		{
			if (ChatPeopleButton.chatPeopleBtnList.Contains(cpBtn))
			{
				ChatPeopleButton.chatPeopleBtnList.Remove(cpBtn);
			}
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/Chat/ChatPeopleButton.xaml", 2));
		}
	}
}
